package com.saikuo.service.service.impl;

import com.saikuo.service.api.ApiResultModel;
import com.saikuo.service.api.ErrorCode;
import com.saikuo.service.constant.ContextConfig;
import com.saikuo.service.constant.JpushConfig;
import com.saikuo.service.constant.OrderStatus;
import com.saikuo.service.dao.ICouponDao;
import com.saikuo.service.dao.IMedicineDao;
import com.saikuo.service.dao.IPlatformDao;
import com.saikuo.service.dao.IStoreInfoDao;
import com.saikuo.service.jpush.PushManage;
import com.saikuo.service.model.Order;
import com.saikuo.service.model.StoreInfo;
import com.saikuo.service.service.IPlatformService;
import com.saikuo.service.utils.EnumUtil;
import com.saikuo.service.vo.CouponView;
import com.saikuo.service.vo.OrderView;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shanchangyu on 16/10/18.
 */
@Service
public class PlatformServiceImpl implements IPlatformService {

    @Autowired
    private IPlatformDao platformDao;
    @Autowired
    private IMedicineDao medicineDao;
    @Autowired
    private IStoreInfoDao storeInfoDao;
    @Autowired
    private ICouponDao couponDao;
    @Autowired
    private JpushConfig jpushConfig;

    @Override
    public ApiResultModel updateOrderInfo(String userId, Long orderId, int status) {
        if (EnumUtil.getEnumByOrdinal(OrderStatus.class, status) == null) {
            return new ApiResultModel(ErrorCode.ORDER_STATUS_FAIL);
        }
        boolean result = platformDao.updateOrderInfo(orderId, status);
        if (!result) {
            return new ApiResultModel(ErrorCode.DATABASE_ERROR);
        }
        //推送给商户平台
        PushManage pushManage =new PushManage();
        pushManage.sendPushOrderAlter(jpushConfig.getAppKey(), jpushConfig.getMasterSecret(), userId, orderId);
        return new ApiResultModel();
    }

    @Override
    public ApiResultModel getOrdersByStoreCode(int storeCode, int orderStatus, int page) {
        int start = page > 0 ? page - 1 : page;
        List<Order> orders = platformDao.getOrdersByStoreCode(storeCode, orderStatus, start * ContextConfig.ORDER_PAGE_SIZE, ContextConfig.ORDER_PAGE_SIZE);
        if (CollectionUtils.isNotEmpty(orders)) {
            List<OrderView> orderViews = new ArrayList<>(orders.size());
            for (Order order : orders) {
                OrderView orderView = new OrderView();
                //药品详情
                orderView.setMedicine(medicineDao.getMedicineDetailByOrderId(order.getOrderId()));
                //药品其他信息
                StoreInfo storeInfo = storeInfoDao.getStoreInfoByStoreCode(order.getStoreCode());
                if (storeInfo != null) {
                    orderView.setStoreName(storeInfo.getName());
                } else {
                    orderView.setStoreName(ContextConfig.DEFAILT_STORE_NAME);
                }
                if (order.getUserCouponId() != null && order.getUserCouponId() != 0) {
                    CouponView couponView = couponDao.getCouponByUserCouponId(order.getUserCouponId());
                    if (couponView != null) {
                        orderView.setCouponPrice(String.valueOf(couponView.getPrice()));
                    }
                }
                orderView.setUserId(order.getUserId());
                orderView.setOrderId(order.getOrderId());
                orderView.setPrice(order.getPrice().toString());
                orderView.setPostage(order.getPostage().toString());
                orderView.setStatus(order.getStatus());
                orderView.setPayWay(order.getPayWay());
                orderView.setDeliveryWay(order.getDeliveryWay());
                orderView.setUpdateTime(order.getUpdateTime());
                orderViews.add(orderView);
            }
            return new ApiResultModel(orderViews);
        }
        return new ApiResultModel();
    }

}
